/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.dao.impl;
import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.NewsAdminDao;
import com.ktjiaoyu.news.entity.NewsAdmin;
import com.ktjiaoyu.news.mapper.NewsAdminMapper;
import com.ktjiaoyu.news.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * 管理员信息表，用于登录后台 
 * @author 科泰集团教研团队
 * @createTime 2024-4-9 17:36:33
 * @lastModify 2024-4-9 17:36:33
 * @version 1.0.0
 */
public class NewsAdminDaoImpl extends BaseDao implements NewsAdminDao {

    @Override
    public NewsAdmin getByUsername(String username) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        NewsAdmin admin = sqlSession.getMapper(NewsAdminMapper.class).getByUsername("admin");
        MyBatisUtil.closeSqlSession(sqlSession);
        return admin;
    }

    @Override
    public List<NewsAdmin> getPage(NewsAdmin entity, Integer pageNo, Integer pageSize) {

        System.out.println("进入了MyBatisUtilDao");

        List<NewsAdmin> list = new ArrayList<>();
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        list= sqlSession.getMapper(NewsAdminMapper.class).getPage(entity, (pageNo-1)*pageSize, pageSize);
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public int count(NewsAdmin entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int count = sqlSession.getMapper(NewsAdminMapper.class).count(entity);
        MyBatisUtil.closeSqlSession(sqlSession);
        return 0;
    }

    @Override
    public List<NewsAdmin> getAll() {
        List<NewsAdmin> list = new ArrayList<>();
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        list = sqlSession.getMapper(NewsAdminMapper.class).getAll();
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public NewsAdmin getById(Long id) {
        NewsAdmin newsAdmin = null;

        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        newsAdmin = sqlSession.getMapper(NewsAdminMapper.class).getById(id);
        MyBatisUtil.closeSqlSession(sqlSession);

        return newsAdmin;
    }

    @Override
    public int insert(NewsAdmin entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int count = sqlSession.getMapper(NewsAdminMapper.class).insert(entity);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public int insertBatch(NewsAdmin... entities) {
        int rows = 0;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        rows = sqlSession.getMapper(NewsAdminMapper.class).insertBatch(entities);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return rows;
    }

    @Override
    public int update(NewsAdmin entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int update = sqlSession.getMapper(NewsAdminMapper.class).update(entity);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return  update;
    }

    @Override
    public int deleteByEntity(NewsAdmin entity)  {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int delete = sqlSession.getMapper(NewsAdminMapper.class).deleteByEntity(entity);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return delete;
    }

    @Override
    public int deleteById(Long id) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int delete = sqlSession.getMapper(NewsAdminMapper.class).deleteById(id);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return delete;
    }

    @Override
    public int deleteBatch(Long... ids) {
        int rows = 0;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        rows = sqlSession.getMapper(NewsAdminMapper.class).deleteBatch(ids);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return rows;
    }
}
